package com.goethe.translator;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.util.Log;

public class DirDerDasHttpRequest extends AsyncTask<Void , Void, ArrayList<String>> {

	@Override
	protected ArrayList<String> doInBackground(Void... arg0) {
    
        // HTTP post
        InputStream is = null;
        try {
        	HttpClient httpclient = new DefaultHttpClient();
        	//HttpPost httppost = new HttpPost("http://traductorgoethe.webege.com/getRandomNoun.php");
        	HttpPost httppost = new HttpPost("http://135.20.205.106/getRandomNoun.php");
        	HttpResponse response = httpclient.execute(httppost);
        	HttpEntity entity = response.getEntity();
        	is = entity.getContent();
        	  
        }catch(Exception e){
        	Log.e("DisplayMessageActivity", "Error in http connection: " + e.toString());
        }

        // Convert response to string
        ArrayList<String> response = new ArrayList<String>();
  		try{
  			BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
  			
  			String temp;
  			while ((temp = reader.readLine()) != null) {
  				response.add(temp);
  			}
			
  		}catch(Exception e){
  			Log.e("log_tag", "Error obtaining results: " + e.toString());
  		}finally{
  			try {
				is.close();
			} catch (IOException e) {
				Log.e("log_tag", "Error obtaining results: " + e.toString());
			} catch (Exception e) {
				Log.e("log_tag", "Generic exception catched: " + e.toString());
			}
  		}
  		
  		return response;
	}

}
